<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Lambda | Joshua Hull</title>
    <link>https://joshua_hull.gitlab.io/tags/lambda/</link>
      <atom:link href="https://joshua_hull.gitlab.io/tags/lambda/index.xml" rel="self" type="application/rss+xml" />
    <description>Lambda</description>
    <generator>Source Themes Academic (https://sourcethemes.com/academic/)</generator><language>en-us</language><copyright>© 2021 Joshua Hull - Licensed under Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.</copyright><lastBuildDate>Mon, 10 Sep 2018 10:22:40 -0400</lastBuildDate>
    <image>
      <url>https://joshua_hull.gitlab.io/images/icon_hu0b7a4cb9992c9ac0e91bd28ffd38dd00_9727_512x512_fill_lanczos_center_2.png</url>
      <title>Lambda</title>
      <link>https://joshua_hull.gitlab.io/tags/lambda/</link>
    </image>
    
    <item>
      <title>Using Scheduled Lambda Functions To Snapshot Volumes</title>
      <link>https://joshua_hull.gitlab.io/post/2018-09-10-lambda-snapshot/</link>
      <pubDate>Mon, 10 Sep 2018 10:22:40 -0400</pubDate>
      <guid>https://joshua_hull.gitlab.io/post/2018-09-10-lambda-snapshot/</guid>
      <description>&lt;p&gt;One of the primary methods I use for backups when working inside AWS is the use of snapshots. They&#39;re straight forward in AWS and they&#39;re available in the AWS CLI which means they are scriptable. In the past I&#39;d write a simple BASH script and schedule it with Cron but this is the year of Serverless so I decided to re-write the process with AWS Lambda. While I&#39;m not going to post the exact code I&#39;ll cover a few of the tidbits below.&lt;/p&gt;
&lt;p&gt;The premise of the script is that it will run on some schedule and describe all volumes in the account it&#39;s operating under. It will then filter out volumes that don&#39;t contain at least one tag that matches a regular expression. This RegEx will make sure we&#39;re working on volumes that contain the schedule the volume should have a snapshot taken on, the last time it was run, the name of the snapshot, etc.&lt;/p&gt;
&lt;p&gt;Given my familiarity with the JavaScript AWS SDK and NodeJS in general I chose to use that framework to develop the lambda script. Since the JS SDK can return Promises I tend to make these types of lambdas a chain of promises, similar to this:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-javascript&#34;&gt;
params = { ... }
EC2.describeVolumes(params).promise().then(data =&amp;gt; {
    let promises = []
    // Magic goes here.
    return Promise.all(promises);
})
.then (data =&amp;gt; {
    let promises = []
    // Even more magic goes here.
    return Promise.all(promises);
})
.then(data =&amp;gt; {
    callback(null, data);
})
.catch(err =&amp;gt; {
    callback(err);
})

&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Fill in the blanks and you have a pretty handy Lambda function that can work with not only the EC2 API but any AWS service that returns promises through the JS SDK. One example is the SNS service. I use this one to send out emails when the snapshot has been preformed but you could just as easily send them out in the &lt;code&gt;catch()&lt;/code&gt; to report errors instead of success.&lt;/p&gt;
&lt;p&gt;One big thing to emphasize though is that most of the configuration happens on the volumes themselves. In my particular use case the volumes have snapshots created varying schedules. This requires setting up multiple cron jobs or lambdas with different configurations. However, with this new lambda each volume can have its own configuration that can be updated with me having to re-deploy the lambda with a new configuration itself.&lt;/p&gt;
</description>
    </item>
    
  </channel>
</rss>
